package sng.modules.view.components.city.strategy
{
	import flash.events.TimerEvent;
	
	import mx.controls.Alert;
	
	import sng.data.info.GlobalInfo;
	import sng.data.info.GlobalTimer;
	import sng.modules.model.vo.BuildingVO;
	import sng.modules.model.vo.UserBuildingVO;
	import sng.modules.view.components.city.logic.attachment.BuildingTipManager;
	import sng.modules.view.components.city.logic.unit.BaseBuilding;
	import sng.modules.view.components.city.state.EnumBuildState;
	import sng.utils.BuildingUtil;
	import sng.utils.IconUtil;
	import sng.utils.TimeUtil;

	/**
	 * 
	 * @author leonliu
	 * 策略类
	 * 
	 */	
	public class Strategy implements IStrategy
	{
		protected var m_building:BaseBuilding;//建筑实例
		protected var m_bvo:BuildingVO;//建筑定义数据
		protected var m_userbvo:UserBuildingVO;//建筑实例数据
		protected var m_itimernum:uint;//倒计时 时间
		
		protected var tip:BuildingTooltip;//提示对象
		protected var tipFrame:String;//提示信息所处的帧
		/**
		 * 
		 * @param bvo 建筑定义数据
		 * @param userbvo 用户建筑实例数据
		 * 
		 */		
		public function Strategy(building:BaseBuilding,bvo:BuildingVO,userbvo:UserBuildingVO)
		{
			m_building=building;
			m_bvo=bvo;
			m_userbvo=userbvo;
			//m_userbvo.countdown = m_bvo.countdown;
			m_userbvo.countdownTime = Number(m_userbvo.countdown)+TimeUtil.getTimeSecond();
			
			//1.设置当前建筑的状态
			buildStateManage(userbvo.currentState);
			if (userbvo.countdown>0 && userbvo.nextState>0)//有倒计时，并且有下一个状态
			{
				if (userbvo.currentState==EnumBuildState.IDLE_STATE)
				{
					GlobalTimer.timeFunctionList.addItem({id:m_bvo.buildingid,time:userbvo.countdown, func:stateTimerHandler,run:false});
				}
				else
					GlobalTimer.timeFunctionList.addItem({id:m_bvo.buildingid,time:userbvo.countdown, func:stateTimerHandler,run:true});
			}
			//2.设置
		}
		
		
		/**
		 *初始化建筑时（从配置文件载入城市数据）
		 * 
		 */		
		public function stateTimerHandler():void
		{
			buildStateManage(m_userbvo.nextState);
		}

		public function buildStateManage(iState:uint):void
		{
			m_building.setBuildState(EnumBuildState.getStateById(iState));
		}
		
		/**
		 * 
		 * @return 返回新建建筑开始后的增量
		 * 
		 */		
		public function createBuildingStrategy():BuildingIncrement
		{
			//1.计算新建开始的数值变化
			GlobalInfo.mainvo.money=GlobalInfo.mainvo.money-m_bvo.buildingprice;
			return new BuildingIncrement();
		}
		
		/**
		 * 
		 * @return 返回新建建筑完成后的增量
		 * 
		 */		
		public function addBuildingStrategy():void
		{
			//新建后建筑变成 normal 
			buildStateManage(EnumBuildState.NORMAL_STATE);
		}
		
		public function clickNormalBuildingStrategy():void
		{
			
		}
		
		/**
		 * 
		 * @return 返回拆除建筑计算后的增量
		 * 
		 *
		 */		
		public function deleteBuildingStrategy():void
		{
		
		}
		
		
		/**
		 * 
		 * @return 返回移动建筑计算后的增量
		 * 
		 */		
		public function moveBuildingStrategy():BuildingIncrement
		{
			return new BuildingIncrement();
		}
		
		/**
		 * 供货后
		 */
		public function supplyGoodsBuildingStrategy():BuildingIncrement
		{
			return new BuildingIncrement();
		}
		
		/**
		 * 收获金币后
		 */
		public function gatherBuildingStrategy():Boolean
		{
			//1.计算新建后的数值变化
			//修改为拾取金币时计算变化 DropAttachment的gatherOver方法 eason 20110719
//			IconUtil.changeValue(GlobalInfo.mainvo,"money",m_bvo.dropCoin*(1+m_userbvo.totalBonusRate))
//			GlobalInfo.mainvo.money +=m_bvo.dropCoin*(1+m_userbvo.totalBonusRate);//每次收获时获取的金钱数量 * 加成
			return true;
		}
		
		/**
		 *收获后出现进度条，进度条完成后才能转换状态 
		 * 
		 */		
		public function gatherProcessLater():void
		{
		}
		
		/**
		 *心跳 
		 * 
		 */		
		protected function heartBeat(event:TimerEvent):void
		{
			
		}
		
		/**
		 *注册心跳 
		 * 
		 */		
		public function hearBeatStart(_tip:BuildingTooltip):void
		{
			tip=_tip;
			GlobalTimer.addHeartBeatEventListener(heartBeat);
			//悬浮上后立即改变显示，而不是过一秒后
			heartBeat(null);
		}
		
		/**
		 *取消心跳 
		 * 
		 */		
		public function hearBeatEnd():void
		{
		
			GlobalTimer.removeHeartBeatEventListener(heartBeat);
			if (tip!=null)
				tip=null;
		}
		
		//7.闲置后
		public function idleBuildingStrategy():void
		{
			if (m_bvo.buildingTypeId==BuildingUtil.HOUSE_TYPE ||m_bvo.buildingTypeId==BuildingUtil.BUSINESS_TYPE )//住宅或商业建筑
			{
				if (m_userbvo.isIdle)//转为空闲
				{
					if (m_userbvo.currentState!=EnumBuildState.NEW_STATE)
					{
						//1.保存当前状态为下一个状态
						m_userbvo.nextState=m_building.currentState.getiStateType();
						//2.将当前状态设置为空闲状态
						buildStateManage(EnumBuildState.IDLE_STATE)
						//m_building.setIdleUI();
					}
					
				}
				else//转为原始的非空闲状态
				{
					buildStateManage(m_building.itempstate);
					GlobalTimer.setFuncRun(m_userbvo.buildingid,true);
				}
				
			}
			
		}
		
		public function setFuncRun(blRun:Boolean):void
		{
			GlobalTimer.setFuncRun(m_userbvo.buildingid,blRun);
		}
	}
}